<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" Title="Apply for Schedule" %>

<%@ Import Namespace="System.Configuration" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="MeetingRoom.Domain" %>
<%@ Import Namespace="MeetingRoom.Data.Implementation" %>
<%@ Import Namespace="MeetingRoom.Service.Workflow" %>
<%@ Import Namespace="MeetingRoom.Service.Workflow.Implementation" %>

<script runat="server">

    protected void ButtonSubmit_Click(object sender, EventArgs e)
    {
        SqlConnection connection 
            = new SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);
        SimpleMeetingRoomTypeQuery meetingRoomTypQuery
            = new SimpleMeetingRoomTypeQuery();
        meetingRoomTypQuery.Connection = connection;
        SimpleScheduleDataQuery scheduleDataQuery =
            new SimpleScheduleDataQuery();
        scheduleDataQuery.Connection = connection;

        IWorkflow workflow = new SimpleWorkflow();
        AvailablityCheckingPhase availableChecking = new AvailablityCheckingPhase();
        availableChecking.GetMeetingRoomTypeImplementor = meetingRoomTypQuery;
        availableChecking.GetSchedulesByMeetingRoomTypeImplementor =
            scheduleDataQuery;
        workflow.AddPreCondition(availableChecking);

        AddSchedule addSchedule = new AddSchedule();
        workflow.AddPostCondition(addSchedule);

        Schedule schedule = new Schedule();
        schedule.Name = this.TextBoxName.Text;

        DateTime selectedDate = this.CalendarDate.SelectedDate;
        int hour = Int32.Parse(this.TextHour.Text);
        int minute = Int32.Parse(this.TextMinute.Text);

        schedule.Duration = new Duration(
            new DateTime(
            selectedDate.Year,
            selectedDate.Month,
            selectedDate.Day,
            hour,
            minute,
            0),
            new DateTime(
            selectedDate.Year,
            selectedDate.Month,
            selectedDate.Day,
            hour,
            minute,
            0)
        );
        MeetingRoomType meetingRoomType = new MeetingRoomType();
        meetingRoomType.Size = Int32.Parse(
            this.DropDownListAvailableRoomSize.SelectedValue);
        meetingRoomType.Projector = Boolean.Parse(
            this.DropDownListProjector.SelectedValue);

        schedule.MeetingRoomType = meetingRoomType;

        this.LabelMessage.Visible = true;
        try
        {
            workflow.Execute(schedule);
            this.LabelMessage.Text = "Schedule is booked.";
            Session["schedule"] = schedule;
            ButtonNext.Visible = true;
        }
        catch (WorkflowException ex)
        {
            Console.WriteLine(ex.StackTrace);
            this.LabelMessage.Text =
                string.Format("Failed to make a schedule. {0}\n{1}\n",
                    ex.Message, ex.StackTrace);
        }
    }

    protected void ButtonNext_Click(object sender, EventArgs e)
    {
        Response.Redirect("Invite.aspx");
    }
</script>
<asp:Content ID="Content1" ContentPlaceHolderID="content" runat="Server">
    <table>
        <tr>
            <td style="width: 124px; height: 12px;">
                Title</td>
            <td style="width: 234px; height: 12px;">
                <asp:TextBox ID="TextBoxName" runat="server" Width="220px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td style="width: 124px; height: 206px">
                Date</td>
            <td style="height: 206px">
                <asp:Calendar ID="CalendarDate" runat="server" BackColor="#FFFFCC" BorderColor="#FFCC66"
                    BorderWidth="1px" DayNameFormat="Shortest" Font-Names="Verdana" Font-Size="8pt"
                    ForeColor="#663399" Height="200px" ShowGridLines="True" Width="220px">
                    <SelectedDayStyle BackColor="#CCCCFF" Font-Bold="True" />
                    <TodayDayStyle BackColor="#FFCC66" ForeColor="White" />
                    <SelectorStyle BackColor="#FFCC66" />
                    <OtherMonthDayStyle ForeColor="#CC9966" />
                    <NextPrevStyle Font-Size="9pt" ForeColor="#FFFFCC" />
                    <DayHeaderStyle BackColor="#FFCC66" Font-Bold="True" Height="1px" />
                    <TitleStyle BackColor="#990000" Font-Bold="True" Font-Size="9pt" ForeColor="#FFFFCC" />
                </asp:Calendar>
            </td>
        </tr>
        <tr>
            <td style="width: 124px">
                Hour</td>
            <td>
                <asp:TextBox ID="TextHour" runat="server" Width="220px"></asp:TextBox></td>
        </tr>
        <tr>
            <td style="width: 124px">
                Minute</td>
            <td>
                <asp:TextBox ID="TextMinute" runat="server" Width="220px"></asp:TextBox></td>
        </tr>
        <tr>
            <td style="width: 124px; height: 9px;">
                Meeting Room Size</td>
            <td style="width: 234px; height: 9px;">
                <asp:DropDownList ID="DropDownListAvailableRoomSize" runat="server" Width="222px"
                    TabIndex="5">
                    <asp:ListItem>10</asp:ListItem>
                    <asp:ListItem>20</asp:ListItem>
                    <asp:ListItem>30</asp:ListItem>
                </asp:DropDownList></td>
        </tr>
        <tr>
            <td style="width: 124px">
                Projector Ready</td>
            <td>
                <asp:DropDownList ID="DropDownListProjector" runat="server" Width="222px" TabIndex="6">
                    <asp:ListItem Text="Yes" Value="True"></asp:ListItem>
                    <asp:ListItem Text="No" Value="False"></asp:ListItem>
                </asp:DropDownList></td>
        </tr>
        <tr>
            <td>
            </td>
            <td style="height: 25px; width: 124px;">
                <asp:Button ID="ButtonSubmit" Text="Apply" runat="server" Width="220px" TabIndex="6"
                    OnClick="ButtonSubmit_Click" /></td>
        </tr>
        <tr>
            <td style="width: 124px; height: 107px">
                <asp:Label ID="LabelMessage" runat="server" Width="101px" Visible="False"></asp:Label></td>
            <td>
                <asp:Button ID="ButtonNext" runat="server" OnClick="ButtonNext_Click" Text="Next (Add Attendees)"
                    Width="223px" /></td>
        </tr>
    </table>
</asp:Content>
